package com.pb.b2b.bean;

import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.pb.b2b.model.Language;
import com.pb.b2b.model.enums.Status;
import com.pb.b2b.service.LanguageService;
import com.pb.b2b.util.FacesUtils;

@Component
@Scope("view")
@ManagedBean(name = "languageBean")
public class LanguageBean extends BaseBean {

	@Autowired
	LanguageService languageService;

	private List<Language> languageList;
	private Language selectedLanguage;
	private boolean status;

	@PostConstruct
	public void init() {
		languageList = languageService.findAll(Language.class);
	}

	/* METHOD */
	@Override
	public String confirmAction() {
		if (StringUtils.isEmpty(selectedLanguage.getName())) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR, "ERROR_LANGUAGE_NAME_CAN_NOT_EMPTY");
			return RETURN_ERROR;
		}

		if (StringUtils.isEmpty(selectedLanguage.getCode())) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR, "ERROR_LANGUAGE_CODE_CAN_NOT_EMPTY");
			return RETURN_ERROR;
		}
		selectedLanguage.setStatus(status ? Status.AKTIVE : Status.PASSIVE);
		return RETURN_SUCCESS;
	}

	@Override
	public String executeAction() {
		selectedLanguage.setDateModified(new Date());
		languageService.saveOrUpdate(selectedLanguage);
		if (hasError()) {
			return RETURN_ERROR;
		}
		return RETURN_SUCCESS;
	}

	public String initChangeLanguage() {
		if (selectedLanguage == null) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR, "VALIDATION_DIL_SECILMELI");
			return RETURN_ERROR;
		}
		status = selectedLanguage.getStatus().equals(Status.AKTIVE) ? true : false;
		return "changeLanguage";
	}

	public String defaultConfirmAction() {
		if (selectedLanguage == null) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR, "VALIDATION_DIL_SECILMELI");
			return RETURN_ERROR;
		}
		if (selectedLanguage.getStatus().equals(Status.PASSIVE)) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR, "VALIDATION_DIL_PASIF_OLMAMALI");
			return RETURN_ERROR;
		}
		if (StringUtils.isNotEmpty(selectedLanguage.getActive())) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR, "VALIDATION_DIL_ZATEN_DEFAULT");
			return RETURN_ERROR;
		}

		return "defaultLanguage";
	}

	public String defaultExecuteAction() {
		selectedLanguage.setDateModified(new Date());
		languageService.setDefaultLanguage(selectedLanguage);
		if (hasError()) {
			return RETURN_ERROR;
		}
		return RETURN_SUCCESS;
	}

	/* GETTER - SETTER */
	public List<Language> getLanguageList() {
		return languageList;
	}

	public void setLanguageList(List<Language> languageList) {
		this.languageList = languageList;
	}

	public Language getSelectedLanguage() {
		return selectedLanguage;
	}

	public void setSelectedLanguage(Language selectedLanguage) {
		this.selectedLanguage = selectedLanguage;
	}

	public final boolean isStatus() {
		return status;
	}

	public final void setStatus(boolean status) {
		this.status = status;
	}
}
